/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.reflection {

	/**
	 * <code>Variable</code> represents a variable of a class. 
	 * 
	 * @author Max Kugland
	 */
	public class Variable implements IMember {
		private var _clazz : Class;
		private var _name : String;
		private var _returntype : String;
		private var _isStatic : Boolean;
		private var _metaData : Array;

		/**
		 * @param name the name of the <code>Variable</code>
		 * @param type the type of the <code>Variable</code>
		 * @param isStatic indicates whether the <code>Variable</code> is static or not
		 * @param metaData optional metadata (annotations)
		 */
		public function Variable(clazz : Class, name : String, returntype : String, isStatic : Boolean, metaData : Array = null) {
			_clazz = clazz;
			_name = name;
			_returntype = returntype;
			_isStatic = isStatic;
			_metaData = metaData;
		}

		/**
		 * @inheritDoc
		 */
		public function get clazz() : Class {
			return _clazz;
		}

		/**
		 * @inheritDoc
		 */
		public function get name() : String {
			return _name;
		}

		/**
		 * @inheritDoc
		 */
		public function get returntype() : String {
			return _returntype;
		}

		/**
		 * @inheritDoc
		 */
		public function get isStatic() : Boolean {
			return _isStatic;
		}

		/**
		 * @inheritDoc
		 */
		public function invoke(instance : Object = null, params : * = null) : * {
			if(params == null) {
				if(_isStatic) 
					return _clazz[_name];
				else return instance[_name];
			} else {
				if(_isStatic) {
					_clazz[_name] = params;
					return _clazz[_name];
				} else {
					instance[_name] = params;
					return instance[_name];
				}
			}
		}

		/**
		 * @inheritDoc
		 */
		public function get metaData() : Array {
			return _metaData;
		}
	}
}
